/* Copyright (c) 2004-2008, Andriy Nikolov
 * All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.processmining.email;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

/**
 * <p>A panel, which contains the drawing of all diagrams in the standalone version</p>
 *
 * @author Andriy Nikolov
 * @version 1.0
 */
public class DiagramPanel extends JPanel {
  ProcessLog processLog;
  Vector arcs;
  Point center;
  DiagramNode nodeBeingCarried = null;

  int maxNumberOfNodes = 0;

  public static final int DIAGRAM_TOPMOST_POINT = 50;
  public boolean showOnlyMaximalArcs = false;
  public static int scale = 1;


  public DiagramPanel() {
    super();
    arcs = new Vector();
    addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
        dropNode(e);
     }
    });
    addMouseMotionListener(new java.awt.event.MouseMotionAdapter(){
      public void mouseDragged(MouseEvent e) {
        carryNode(e);
      }
    });
  }

  public void setShowOnlyMaximalArcs(boolean value) {
    showOnlyMaximalArcs = value;
  }

  public void setProcessLog(ProcessLog log) {
    processLog = log;
  }

  public ProcessLog getProcessLog() {
    return processLog;
  }

  public void paintComponent(Graphics g) {
    DiagramArc iterArc;
    super.paintComponent(g);
    for(int i=0;i<arcs.size();i++) {
      iterArc = (DiagramArc)arcs.get(i);
      iterArc.paint(g);
    }
  }

  private void calculateCenter() {
    center = new Point();
    center.x = new Double(getSize().getWidth()/2).intValue();
    center.y = new Double(getSize().getHeight()/2).intValue();
//    radius = center.y-20;
  }

  public void setSize(int width, int height) {
    super.setSize(width, height);
    calculateCenter();
  }

  public void setSize(Dimension d) {
    super.setSize(d);
    calculateCenter();
  }

  private void dropNode(MouseEvent e) {
//    System.out.println("carry: "+e.getX()+":"+e.getY());
    if(nodeBeingCarried!=null) {
      nodeBeingCarried.stopCarry(e);
    }
  }

  private void carryNode(MouseEvent e) {
//    System.out.println("carryNode: "+e.getX()+":"+e.getY());
    if(nodeBeingCarried!=null) {
      nodeBeingCarried.carry(e);
    }
  }

  public void setNodeBeingCarried(DiagramNode node) {
    nodeBeingCarried = node;
  }

  public DiagramNode getNodeBeingCarried() {
    return nodeBeingCarried;
  }
}
